
var myScroll,
	//pullDownEl, 
	pullDownOffset,
	pullUpEl, pullUpOffset,
	generatedCount = 0,
	nextPage = 2;

 

function pullUpAction () {
 
	setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
		if(nextPage > 0){
			var brokerageDtl=Handlebars.compile($('#brokerageDtl').html());
			$.ajax({ 
	            url: path+'/agent/getBrokerageList',
	            type: 'POST',
	            data: {page:nextPage,rows:20} ,
	            cache:false,
	            dataType:'json',  
	            success: function(data){ 
	            	var html = brokerageDtl(data.rows);
	            	console.info(html);
	            	$('#wrapper ul:first-child').append(html);
	            	nextPage++;
	            	if(data.rows.length<20){
	            		nextPage = -1;
	            	}
	            	$('#pullUp').hide();
	            },
	            error : function(data) {    
	                console.info(data);
	            }    
	         }); 
		}else{
			$('#loaderBall').html().hide();
			$('#loadingWord').text('已无更多数据');
		}
		myScroll.refresh();		// Remember to refresh when contents are loaded (ie: on ajax completion)
	}, 1000);	// <-- Simulate network congestion, remove setTimeout from production!
}

function loaded() { 
	pullUp = $('#pullUp');	
	pullUpEl = document.getElementById('pullUp');	
	pullUpOffset = pullUpEl.offsetHeight;
	
	myScroll = new iScroll('wrapper', {
		useTransition: true,
		topOffset: pullDownOffset,
		onRefresh: function () {
			//alert('onRefresh');
			if (pullUp.hasClass('loading')) {

				//alert('onRefresh next');
				pullUp.show();
				pullUp.removeClass('flip').removeClass('loading'); 
				$('#releaseWord').hide();
				$('#loaderBall').show();
				$('#loadingWord').show();
			}
		},
		onScrollMove: function () {
			if (this.y < (this.maxScrollY - 5) && !pullUp.hasClass('flip')) {
				//alert(-5);
				pullUp.show(); 
				pullUp.addClass('flip');
				$('#loaderBall').hide();
				$('#loadingWord').hide();
				$('#releaseWord').show();
				//pullUpEl.className = 'flip';
				//pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 5) && pullUp.hasClass('flip')) {
				//alert(5);
				pullUp.show(); 
				pullUp.removeClass('flip').removeClass('loading'); 
				$('#releaseWord').hide();
				$('#loaderBall').show();
				$('#loadingWord').show();
				//pullUpEl.className = '';
				//pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
				this.maxScrollY = pullUpOffset;
			}
		},
		onScrollEnd: function () {

			//alert('onScrollEnd');
			 if (pullUp.hasClass('flip')) {
				 //alert('onScrollEnd next');
				 pullUp.show();
				 pullUp.addClass('loading');
				 $('#loaderBall').hide();
				 $('#loadingWord').hide();
				 $('#releaseWord').show();			
				pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	});
	
	setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);